<?php
/**
 * Created by PhpStorm.
 * User: yxb
 * Date: 2016/12/19
 * Time: 14:47
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cache;
class User extends Controller{
    
   
    /**
    订单
     * @param status 1全部2待支付3已支付4待评论5退款
     * @param uid
     * @param assess_token
     */
    public function orders($uid = 0,$assess_token = '',$status = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where_order = [
                'uid' => $uid
            ];
            switch($status){
                case 1:
                    break;
                case 2:
                    $where_order['o.status'] = 0;
                    break;
                case 3:
                    $where_order['o.status'] = array('in','1,2');
                    break;
                case 4:
                    $where_order['o.status'] = 3;
                    break;
                case 5:
                    $where_order['o.status'] = 6;
                    break;
                default:
                    break;
            }
            $join = [
                ['goods g','g.id=o.g_id','left']
            ];
            $field = [
                'o.order_id',
                'o.ids',
                'o.status',
                'o.price',
                'o.integral',
                'o.create_timestamp',
                'o.order_num',
                'o.g_id',
                'g.subject',
                'g.cover'
            ];
            $ass = '';
            $orders = Db::name("orders")->alias('o')->join($join)->field($field)->where($where_order)->order('create_timestamp desc')->select();
            foreach($orders as $key => $value){
                $orders[$key]['create_timestamp'] = date('Y-m-d H:i:s',$value['create_timestamp']);
                $assessment = Db::name("goods_assessment")->where('is_delete',0)->where('id','in',explode(',',$value['ids']))->select();
                foreach($assessment as $keys => $values){
                    $ass .= $values['attribute'].' '.$values['assessment'].' ';
                }
                $orders[$key]['assessment'] = $ass;
                $ass = '';
            }
            return format_return(1,$orders);
        }
    }

    /**
    我的收藏
     * @param uid
     * @param assess_token
     */
    public function mycollection($uid = 0,$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where = [
                'is_delete' => 0,
                'uid' => $uid
            ];
            $data = Db::name("collection")->where($where)->select();
            if(!empty($data)){
                $ids = [];
                foreach($data as $key => $value){
                    $ids[] = $value['g_id'];
                }
                $where_goods = [
                    'is_delete' => 0,
                ];
                $where_goods['id'] = array('in',$ids);
                $goods = Db::name("goods")->where($where_goods)->select();
                return format_return(1,$goods,"数据获取成功");
            }else{
                return format_return(1,'','数据获取成功');
            }
        }
    }
    /**
    收藏
     * @param uid
     * @param assess_token
     * @param g_id
     */
    public function collect_goods($uid = 0,$assess_token = '',$g_id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $data = [
                'uid' => $uid,
                'g_id' => $g_id,
                'timestamp' => time()
            ];
            Db::name("collection")->insert($data);
            return format_return(1,'',"收藏成功");
        }
    }

    /**
    取消收藏
     *  @param uid
     * @param assess_token
     * @param g_id
     */
    public function delete_collection($uid = 0,$assess_token = '',$g_id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where = [
                'uid' => $uid,
                'g_id' => $g_id,
                'is_delete' => 0
            ];
            Db::name("collection")->where($where)->update(['is_delete'=>1]);
            return format_return(1,'',"取消成功");
        }
    }

    /**
    提现
     * @param uid
     * @param assess_token
     * @param cash 金额
     * @param type 0微信1支付宝
     * @param account 账号
     */
 public function cash($uid = '',$assess_token = '', $cash = 0,$type = 0,$account = '',$pswd=''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $member = Db::name("members")->where('uid',$uid)->find();
            if($member['balance'] < $cash){
                return format_return(0,'','提现金额大于余额');
                
            }else{
               if(is_numeric($account)){
                   if(check_phone($account)==false){
                        return format_return(2, '', "支付宝账号格式不正确");
                    }
                }else{
                    
                    if(filter_var($account, FILTER_VALIDATE_EMAIL)==false){
                        return format_return(2, '', "支付宝账号格式不正确");
                    }
                }
                $payment = Db::name("pay")->where('uid',$uid)->field('payment')->find();
                if($payment['payment']!=md5($pswd)){
                    return format_return(0, '', "支付密码错误");
                }
                $data = [
                    'uid' => $uid,
                    'cash' => $cash,
                    'account' => $account,
                    'pay_type' => $type,
                    'timestamp' => time(),
                    'status' => 0
                ];
                $res = Db::name("withdrawals")->insert($data);
                $mem = Db::name('members')->where(array('uid'=>$uid))->find();
                $yue = $mem['balance'] - $cash;
                Db::name('members')->where(array('uid'=>$uid))->update(['balance'=>$yue]);
                if(!empty($res)){
                    return format_return(1,'','提现申请已提交');
                }else{
                    return format_return(0,'','申请失败');
                }
            }
        }
    }


    /**
    验证提现
     * @param uid
     * @param assess_token
     */
    private function test_cash($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            //借贷
            $loan_cash = 0;
            $where_loan = [
                'is_delete' => 0,
                'status' => 1
            ];
            $loan = Db::name("loan")->where($where_loan)->select();
            foreach($loan as $key => $value){
                $loan_cash += $value['price'];
            }
            //退款
            $order_cash = 0;
            $where_order = [
                'status' => 7
            ];
            $orders = Db::name("orders")->where($where_order)->select();
            foreach($orders as $key => $value){
                $order_cash += $value['price'];
            }
            $cash = $loan_cash + $order_cash;
            $member = Db::name("members")->where('uid',$uid)->find();
            if($cash == $member['balance']){
                return format_return(1,'',"验证成功");
            }else{
                return format_return(0,'',"实际金额与现有金额不匹配,请联系官方");
            }
        }
    }
    /**
    个人设置
     * @param uid
     * @param assess_token
     */
    public function personal_setting($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $member = Db::name("members")->where('uid',$uid)->find();
            $where_address = [
                'is_sure' => 1,
                'is_delete' => 0,
                'uid' => $uid
            ];
            $address = Db::name("address")->where($where_address)->find();
            if(empty($address)){
                $where_address['is_sure'] = 0;
                $address = Db::name("address")->where($where_address)->find();
            }
            if(!empty($address)){
                $str_address = $address['province'].$address['city'].$address['area'].$address['address'];
                $member['address'] = $str_address;
            }
            unset($member["password"]);
            return format_return(1,$member);
        }
    }
    public function sss($ppic = ''){
        Db::name("log")->insert(["name"=>$ppic]);
    }
    /**
    修改设置
     * @param uid
     * @param assess_token
     * @param username
     * @param sex
     * @param phone
     * @param ppic
     */
    public function setting($uid = '',$assess_token = '',$username = '',$sex = '',$phone = '',$ppic = ''){
        header('Access-Control-Allow-Origin:*');
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
             Db::name("log")->insert(['name' => $username]);

            if(!empty($ppic)){
                $data = [
                    'username' => $username,
                    'sex' => $sex,
                    'phone_new' => $phone,
                    'logo' => $ppic
                ];
            }else{
                $data = [
                    'username' => $username,
                    'sex' => $sex,
                    'phone_new' => $phone
                ];
            }
            Db::name("members")->where('uid',$uid)->update($data);
            return format_return(1,'',"修改成功");
         }
    }
    /**
    地址选择
     * @param uid
     * @param assess_token
     */
    public function address($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where_address = [
                'is_delete' => 0,
                'uid' => $uid
            ];
            $address = Db::name("address")->where($where_address)->order('is_sure desc')->select();
            return format_return(1,$address);
        }
    }
   /**
   设置默认地址
    * @param uid
    * @param assess_token
    * @param id
    */
    public function set_address($uid = '',$assess_token = '',$id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where_address = [
                'is_delete' => 0,
                'uid' => $uid
            ];
            Db::name("address")->where($where_address)->update(['is_sure' => 0]);
            $where_address['id'] = $id;
            $result = Db::name("address")->where($where_address)->update(['is_sure'=>1]);
            if($result != 0){
                return format_return(1,'',"修改成功");
            }else{
                return format_return(0,'',"修改失败");
            }
        }
    }
    /**
    编辑地址
     * @param uid
     * @param assess_token
     * @param username
     * @param phone
     * @param province
     * @param city
     * @param area
     * @param address
     * @param is_sure
     */
    public function edit_address($uid = '',
                                 $assess_token = '',
                                 $username = '',
                                 $phone = '',
                                 $province = '',
                                 $city = '',
                                 $area = '',
                                 $address = '',
                                 $is_sure = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $data = [
                'uid' => $uid,
                'username' => $username,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'address' => $address,
                'phone' => $phone,
                'timestamp' => time(),
                'is_sure' => $is_sure
            ];
            $where=array('uid'=>$uid ,'is_sure'=>1);
            $sting =Db::name("address")->where($where)->find();
            if($sting && $is_sure==1){
                Db::name("address")->where('id',$sting['id'])->update(['is_sure'=>0]);
            }
           
            $res = Db::name("address")->insertGetId($data);

            if(!empty($res)){
                return format_return(1,'',"添加成功");
            }else{
                return format_return(0,'',"添加失败");
            }
        }
    }
    /**
    修改订单地址
     * @param uid
     * @param assess_token
     * @param id  地址id
     * @param order_num
     * @param order_id
     * @param is_car
     */
    public function update_num_address($uid = '',$assess_token = '',$id = 0,$order_num = 0,$order_id = 0,$is_car = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
           
            if($is_car == 1){
                //多订单
                Db::name("orders")->where("order_num",$order_num)->update(['address'=>$id]);
            }else{
                Db::name("orders")->where('order_id',$order_id)->update(["address"=>$id]);
            }
            return format_return(1,'',"修改成功");
        }
    }
    /**
    删除地址
     * @param uid
     * @param assess_token
     * @param id  地址id
     */
    public function delete_address($uid = '',$assess_token = '',$id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            if(empty($id)){
                return format_return(0,'','非法参数');
            }else{
                Db::name("address")->where('id',$id)->update(['is_delete'=>1]);
                return format_return(1,'','删除成功');
            }
        }
    }

    /**
    修改密码
     * @param uid
     * @param assess_token
     * @param old_password
     * @param new_password
     */
    public function update_password($uid = '',$assess_token = '',$old_password = '',$new_password = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where = [
                'uid' => $uid,
                'password' => md5($old_password)
            ];
            $sting =Db::name("members")->where($where)->find();
            if(empty($sting)){
                return format_return(0,'',"修改失败");
            }else{
                Db::name("members")->where($where)->update(['password' => md5($new_password)]);
                return format_return(1,'',"修改成功");
            }
        }
    }
    
   
 /**
     * 发送验证码:支付密码
     * @param string $phone
     */
    public function get_code($phone = '') {
        empty($phone) && $this->error('非法参数');
        check_phone($phone) || $this->error('手机号格式错误');
        if(Cache::get('code_' . $phone)){
            return format_return(0,'','请求太频繁，请稍后再试');
        }
        $code = rand(100000, 999999);
        Cache::set('code_' . $phone, $code, 60);
        $data_code=send_sms_pay($phone, $code);
        if($data_code==0){
        return format_return(1,'',"发送成功");
        }else{
            return format_return(0,'',"短信发送失败");
        }
    }
    
    //添加支付密码
    public function  add_pay($uid="",$assess_token='',$new_password='',$email='',$code='',$phone=''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where_arr = [
                'uid' => $uid,
                'payment' => md5($new_password),
                'email' =>$email
    
            ];
            $sting=intval($new_password);
            $cache = Cache::get('code_' . $phone);
            // check_code($phone,$code) || $this->error("验证码错误");
            if($cache != $code){
                return format_return(0,'','验证码错误');
            }
            if(strlen($new_password)==6 && !is_int($sting)){     //如果$new_password的长度小于6位
                return format_return(0,'',"密码格式不正确");
            }
            $data=Db::name("pay")->insertGetId($where_arr);
            if($data){
                return format_return(1,'',"添加成功");
            }else{
                return format_return(0,'',"添加失败");
            }
        }
    
    }
    
    //修改支付密码
    public function edit_pay($uid="",$assess_token='',$new_password='',$email='',$code='',$phone=''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where_arr = [
                'uid' => $uid,
            ];
            $sting=intval($new_password);
            if(strlen($new_password)==6 && !is_int($sting)){     //如果$new_password的长度小于6位
                return format_return(0,'',"密码格式不正确");
            }
            $sting =Db::name("pay")->where($where_arr)->field('email')->find();
            if($sting['email']!=$email){
                return format_return(0,'',"邮箱不正确");
            }
            $pay_name=Db::name("pay")->where(array('uid'=>$uid))->field('payment')->find();
            if($pay_name['payment']==md5($new_password)){
                return format_return(0,'',"您修改密码和之前密码相同请您更换");
            }
//             $cache = Cache::get('code_' . $phone);
//             if($cache != $code){
//                 return format_return(0,'','验证码错误');
//             }
            $data=Db::name("pay")->where(array('uid'=>$uid))->update(array('payment'=>md5($new_password)));
            if($data){
                return format_return(1,'',"修改成功");
            }else{
                return format_return(0,'',"修改失败");
            }
        }
    }
    
    
    //验证是否有支付密码
    public function verify_apy($uid="",$assess_token=''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $data =Db::name("pay")->where(array('uid'=>$uid))->field('payment')->find();
            if($data){
                return format_return(1,array('type'=>1),"数据获取成功");
            }else{
                return format_return(1,array('type'=>0),"数据获取成功");
            }
        }
    }

    /**
    意见反馈
     * @param uid
     * @param assess_token
     * @param content
     */
    public function feedback($uid = '',$assess_token = '',$content = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $data = [
                'uid' => $uid,
                'content' => $content,
                'timestamp' => time()
            ];
            $res = Db::name("feedback")->insertGetId($data);
            if(!empty($res)){
                return format_return(1,'',"提交成功");
            }else{
                return format_return(0,'',"提交失败");
            }
        }
    }

    /**
    供应商申请
     * @param uid
     * @param assess_token
     * @param name
     * @param address
     * @param username
     * @param phone
     * @param aptitude
     */
    public function store_line($uid = '',
                                 $assess_token = '',
                                 $name = '',
                                 $address = '',
                                 $username = '',
                                 $phone = '',
                                 $aptitude = ''){
        header('Access-Control-Allow-Origin:*');
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $status[] = 0;
            $status[] = 1;
            $where = [
                'uid' => $uid,
                'status' => array('in',$status),
            ];
            $st = Db::name("store_online")->where($where)->select();
            if(!empty($st)){
                $data['code'] = 0;
                $data['msg'] = "已经申请过供应商，请勿重复申请";
                return json($data);
                //return format_return(0,'',"已经申请过供应商，请勿重复申请");
            }else{
                $where['is_pay'] = 1;
                $ss = Db::name("store_unline")->where($where)->select();
                if(!empty($ss)){
                    $data['code'] = 0;
                    $data['msg'] = "已经申请过线下门店，请等待结果再申请供应商";
                    return json($data);
                   // return format_return(0,'',"已经申请过线下门店，请等待结果再申请供应商");
                }else{
                    $data = [
                        'uid' => $uid,
                        'name' => $name,
                        'address' => $address,
                        'username' => $username,
                        'phone' => $phone,
                        'aptitude' => $aptitude
                    ];
                    $res = Db::name("store_online")->insertGetId($data);
                    if(!empty($res)){
                        $data['code'] = 1;
                        $data['msg'] = "申请成功";
                        return json($data);
                    }else{
                        $data['code'] = 0;
                        $data['msg'] = "申请失败，请重试";
                        return json($data);
                    }
                }
            }
        }
    }

    /**
    线下门店申请
     * @param uid
     * @param assess_token
     * @param name
     * @param address
     * @param username
     * @param phone
     * @param invitation
     * @param trading_type 0 wx 1 alipay
     */
    public function store($uid = '',
                               $assess_token = '',
                               $name = '',
                               $address = '',
                               $username = '',
                               $phone = '',
                               $invitation = '',
                               $trading_type = ''){
        //查看推荐人是否存在
        $re = Db::name('store_unline')->where(array('phone'=>$invitation,'status'=>1))->find();
        //查看自己是否注册过门店
        $user_unline = Db::name('store_unline')->where(array('uid'=>$uid,'is_pay'=>1))->find();
        //var_dump($re);exit;
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            if(empty($re) &&  $invitation !=''){
                return format_return(0,'',"该推荐人不存在!",1);
            }
            if(!empty($user_unline)){
                return format_return(0,'',"您已经申请过门店请等待审核!",1);
            }
            //$status[] = 0;
            //$status[] = 1;
            $where = [
                'uid' => $uid,
                'status' => 0,
            ];
            $st = Db::name("store_online")->where($where)->select();
           
            
           
            if(!empty($st)){
                return format_return(0,'',"已经申请过供应商，请等待结果再申请线下门店");
            }
           
//             $where['is_pay'] = 1;
//             $ss = Db::name("store_unline")->where($where)->find();
//             if(!empty($ss)){
//                 $order_id = $ss['order_id'];
//                 $data = [
//                     'name' => $name,
//                     'address' => $address,
//                     'username' => $username,
//                     'phone' => $phone,
//                     'recommend' => $invitation,
//                     'is_pay' => 0,
//                 ];
               
//                
                //return format_return(0,'',"已经申请过线下门店，请勿重复申请");
        //    }else{
                $order_id = $order_num = str_pad(str_replace('.', '', microtime(true)), 14, 0);
                $re_unline = Db::name('store_unline')->where(array('uid'=>$uid))->field('uid')->find();
                $data = [
                    'uid' => $uid,
                    'name' => $name,
                    'address' => $address,
                    'username' => $username,
                    'phone' => $phone,
                    'recommend' => $invitation,
                    'is_pay' => 0,
                    'order_id' => $order_id
                ];
                if($re_unline){
                    //修改门店信息
                   $res= Db::name("store_unline")->where('uid',$uid)->update($data);
                }else{
                    //新增门店
                    $res = Db::name("store_unline")->insertGetId($data);
                } 
               
        }   
         $price = Db::name("unline_price")->where('id',1)->find();
            //支付
            if($trading_type == 0){
                //微信支付
                vendor('wechatpay.lib.WxPayApi');
                // $total = $order['price'] - $member['balance'];
                $time = time();
                $input = new \WxPayUnifiedOrder();
                $input->SetBody("线下门店申请");
                $input->SetOut_trade_no($order_id);
                $input->SetTotal_fee($price['price']*100);
                $input->SetTime_start(date("YmdHis"));
                $input->SetTime_expire(date("YmdHis", $time + 1800));
                $input->SetTrade_type("APP");
                $input->SetNotify_url("www.tianjunhai.com/index/pay/unline_wx_notify");
                $order_param = \WxPayApi::unifiedOrder($input);
                return format_return(1, $order_param, "参数获取成功");
            }else{
                //支付宝支付
                // 支付宝合作者身份ID，以2088开头的16位纯数字
                $partner = "2088021878456483";
                // 支付宝账号
                $seller_id = '370562505@qq.com';
                // 商品网址
                $base_path = urlencode('http://www.tianjunhai.com');
                // 异步通知地址
                $notify_url = urlencode('http://www.tianjunhai.com/index/pay/unline_alipay_notify');
                // $total = $total - $member['balance'];
                $parameter = array(
                    'service'        => 'mobile.securitypay.pay',   // 必填，接口名称，固定值
                    'partner'        => $partner,                   // 必填，合作商户号
                    '_input_charset' => 'UTF-8',                    // 必填，参数编码字符集
                    'out_trade_no'   => $order_id,              // 必填，商户网站唯一订单号
                    'subject'        => "线下门店申请",                   // 必填，商品名称
                    'payment_type'   => '1',                        // 必填，支付类型
                    'seller_id'      => $seller_id,                 // 必填，卖家支付宝账号
                    'total_fee'      => 0.01,                     // 必填，总金额，取值范围为[0.01,100000000.00]
                    'body'           => "线下门店申请",                      // 必填，商品详情
                    'it_b_pay'       => '1d',                       // 可选，未付款交易的超时时间
                    'notify_url'     => $notify_url,                // 可选，服务器异步通知页面路径
                    'show_url'       => $base_path,                  // 可选，商品展示网站
                    'sign_type'      => 'RSA'
                );
                //生成需要签名的订单
                $orderInfo = $this->createLinkstring($parameter);
                //签名
                $sign = $this->rsaSign($orderInfo);
                //生成订单
                $order_param = $orderInfo.'&sign="'.$sign.'"&sign_type="RSA"';
                return format_return(1,$order_param,"数据获取成功");
            }
//            if(!empty($res)){
//                return format_return(1,$res,"申请成功");
//            }else{
//                return format_return(0,'',"申请失败，请重试");
//            }
        }
    
    // 对签名字符串转义
    private function createLinkstring($para) {
        $arg  = "";
        while (list ($key, $val) = each ($para)) {
            $arg.=$key.'="'.$val.'"&';
        }
        //去掉最后一个&字符
        $arg = substr($arg,0,count($arg)-2);
        //如果存在转义字符，那么去掉转义
        if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
        return $arg;
    }
// 签名生成订单信息
    private function rsaSign($data) {
        $priKey = "-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDg5NZ42g9QvqIV03LJZUFm4MLTADV1VrsJx2nSRskVcBud1XHv
sBWWnAD8779PDxJaCzZtQn0AtEKGy8demmczRZrn7mu6yv3WHfsfg5xVOzk+mWDD
N6+fDD+BrfxT1QO6hq/FaBy8p/G6TmGY8RriZgdEPVHCjyfernJ/ZExEJwIDAQAB
AoGAKu6knteJ+vEZgi8mh5nj3V7k8mDk06yl8hz2b6UBuCz5qKzZnlcwF9taOO8P
x2jqKPRhBISf1mWTGaaHPeEd+AkZ/SH4YZ+596dMw+pYF9GTH4CQ8QuFZymr+3ZO
jMjXsjMjdGYM7RNhQunqU0Ak4bGMC0F4cQQ64gBhqJrypbkCQQD2USUIXaR/KXin
4CB5TOKDFLigGjlcqW9DzZ8VIBFQi+AW8luDmaxeLz+y97zx2NrSIPF0uB7Ki9R3
Zs4J+WZVAkEA6bwZIFc6AAvz2f4JwpdEMg5xeigUG5fSNXFkLU/ZByDuCt6NE7DQ
ZkbcrYR6HicgGlciZqJjMllpMbZI4cbkiwJBAMkUkzPSJmC8/UemuzvrHRs/Rj1F
S0Cu/SsP6Gd5h0kG90gkT7sOL92vmsPCZJfWnURfKl5LlK9rGWbkIR2ZYQUCQQDQ
0rTAKC4xjHw47rZZnDvZXP4VXWlZnbK4/K27rfMAY3NJUrxIM1h9iCHxBML13Y7Y
+bBKchqeCTPqrik/TisxAkBFfuP37iYtC13lWBcCMmI2hB15wEyW6EtZagjLUSo6
jo/97UB+lVSHI/mpYrFu0qtBk1waxF6jRZ1XgxRKoZ0M
-----END RSA PRIVATE KEY-----";
//        $priKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALOAicUPRZ0Hru8w43A7DIEZjVSrqTrRJoaYojr5hXgdsEobqeFCL31alKqz8KMtS9gxO2gkZtBsj1GsajYFiIrz3FUAeOSh6xxPOZCS82aqIxGmeBUUUcHtgvS2dyIva1Zt9S6vdBF4TNWFE2m9tvrqfENsUjoN6HdBdPIkD8+3AgMBAAECgYAsxreXLIQU88GzcOKLMG+iFJmosVl5joqpsJFnXK7qk51SHyx1QGlQP7QuEMzKJ5Zvy3giNlJfU3U8zmGAMEkq1ONS08/JVmLMndLxiRaWfnES76eUz01Y6ZxZC4YpaWsxzDleVrh2h57rRb63qiRhXLdNi5GrJw0DMQKgN/YCYQJBAOpI4Lhjjv59+xbehxya5MMUgstbZf2YQZwVf90V7P46QqVxKsLd09mHyoliCSM6IlhB89r406TU6vl/y006dYsCQQDEI8aZ2N8IBSq7NmdrEau6dzQ1NUsS7r1n9RaE1NJ7WF0ECNRhfMpNkZJ1PcnyThA9J8wb6n0i+3XDMzTUWwwFAkAzy8Lq4Q/nEcEmUDI8z73Np0Y3YVCOHVA8CsDHBybrGcRMQVW72UER8aSEdQkiIaMgMgyQl7xqz6vXVzqCK297AkBLQ18mEe4jabgn9oxgrXs0JiHGeRjBvxK3HXjyp6fM5O9saOb2Mah/c2i7zGX9sK7SiL7tx2EVV2Cs8q1G/1jxAkEAsQCC62m1yS0ije/gBwfOx7M9U3J8a1vpxPzH408TprtOz4xxGAjZU7D05v6FQUi/g5Z7LhFQaiVfuQXyPDpGDg==";
//        $priKey = "-----BEGIN RSA PRIVATE KEY-----\n" .
//            wordwrap($priKey, 64, "\n", true) .
//            "\n-----END RSA PRIVATE KEY-----";
        $res = openssl_get_privatekey($priKey);
        openssl_sign($data, $sign, $res);
        openssl_free_key($res);
        $sign = base64_encode($sign);
        $sign = urlencode($sign);
        return $sign;
    }
    /**
    订单详情
     * @param uid
     * @param assess_token
     * @param order_id
     */
    public function detail($uid = '',$assess_token = '',$order_id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $order = Db::name("orders")->where('order_id',$order_id)->find();
            $address = Db::name("address")->where('id',$order['address'])->find();
            $goods = Db::name("goods")->where('id',$order['g_id'])->find();
            $order['create_timestamp'] = date('Y-m-d H:i:s',$order['create_timestamp']);
            $order['pay_timestamp'] = date('Y-m-d H:i:s',$order['pay_timestamp']);
            $order['send_timestamp'] = date('Y-m-d H:i:s',$order['send_timestamp']);
            $data['order'] = $order;
            $data['address'] = $address;
            $data['goods'] = $goods;
            return format_return(1,$data,"数据获取成功");
        }
    }

    /**
    订单退款
     * @param uid
     * @param assess_token
     * @param order_id
     * @param refund
     * @param refund_reason
     */
    public function refund($uid = '',$assess_token = '',$order_id = 0,$refund = '',$refund_reason = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $update = [
                'refund' => $refund,
                'refund_reason' => $refund_reason,
                'status' => 6
            ];
            Db::name("orders")->where('order_id',$order_id)->update($update);
            return format_return(1,'',"退款申请已受理");
        }
    }

    /**
    取消订单
     * @param uid
     * @param assess_token
     * @param order_id
     */
    public function cancel($uid = '',$assess_token = '',$order_id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where =[
                'is_cancel'=>1,
                'status' => 5
            ];
            Db::name("orders")->where('order_id',$order_id)->update($where);
            return format_return(1,'',"取消成功");
        }
    }

    /**
    确认收货
     * @param uid
     * @param assess_token
     * @param order_id
     */
    public function confirm($uid = '',$assess_token = '',$order_id =""){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where =[
                'status' => 3
            ];
            $res = Db::name("orders")->where('order_id',$order_id)->update($where);
          
           // Db::name("log")->insert(['name'=>$res]);

           if(!empty($res)){
                $order = Db::name("orders")->where('order_id',$order_id)->find();
                //医美商城
                if($order['m_id'] == 1) {
                    //线上分润0.4
                    $profit_online = $order['price'] * 0.4;
                    $where_online = [
                        'online_id' => $order['s_id'],
                        'type' => 3
                    ];
                    $member_online = Db::name("members")->where($where_online)->find();
                    $member_online['balance'] = $member_online['balance'] + $profit_online;
                    Db::name("members")->where($where_online)->update(['balance' => $member_online['balance']]);
                    //门店利润
                    $member = Db::name("members")->where('uid', $order['uid'])->find();
                    $where_unline = [
                        's_id' => $member['store_id'],
                        'type' => 2
                    ];
                    $member_unline = Db::name("members")->where($where_unline)->find();
                    $store_unline = Db::name("store_unline")->where('id', $member['store_id'])->find();
                    $profit_unline = $order['price'] * $store_unline['profit'];
                    $member_unline['balance'] = $member_unline['balance'] + $profit_unline;
                    Db::name("members")->where($where_unline)->update(['balance' => $member_unline['balance']]);
                //名品商城
                }elseif($order['m_id'] == 2){
                    //$member = Db::name("members")->where('uid', $order['uid'])->find();
                    
//                     $where_unline = [
//                         's_id' => $member['store_id'],
//                         'type' => 2
//                     ];
                   // $member_unline = Db::name("members")->where($where_unline)->find();
                    $store_unline = Db::name("store_online")->where('id', $order['s_id'])->find();
                    
                    if(($store_unline['edit_time']+3600*24*180)<=time()){
//                         $profit_unline = $order['price'];
//                         $member_unline['balance'] = $member_unline['balance'] - $profit_unline;
//                         Db::name("members")->where($where_unline)->update(['balance' => $member_unline['balance']]);

                        //$store_unline = Db::name("orders")->where(array('order_id'=>$order_id))->find();
                        $m  = Db::name('store_online')->where(array('id'=>$order['s_id']))->find();
                        $o = Db::name('members')->where(array('uid'=>$m['uid']))->find();
                        $mo = $o['balance'] +$order['price']*0.95;
                        Db::name('members')->where(array('uid'=>$m['uid']))->update(['balance'=>$mo]);
                    }else{
                       // $member_unline['balance'] = $member_unline['balance'] - $order['price'];
                       // Db::name("members")->where($where_unline)->update(['balance' => $member_unline['balance']]);
                        //$store_unline = Db::name("orders")->where(array('order_id'=>$order_id))->find();
                        $m  = Db::name('store_online')->where(array('id'=>$order['s_id']))->find();
                        $o = Db::name('members')->where(array('uid'=>$m['uid']))->find();
                        $mo = $o['balance'] + $order['price'];
                        Db::name('members')->where(array('uid'=>$m['uid']))->update(['balance'=>$mo]);

                    }
                }
                return format_return(0, "1", "确认收货成功");
                //return format_return(1,'',"确认收货成功");
             }else{
                 return format_return(0,'',"失败");
             }

        }
    }
    /**
    获取商品详情
     * @param uid
     * @param assess_token
     * @param order_id
     */
    public function get_goods($uid = '',$assess_token = '',$order_id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $order = Db::name("orders")->where('order_id',$order_id)->find();
            $goods = Db::name("goods")->where('id',$order['g_id'])->find();
            return format_return(1,$goods,"数据获取成功");
        }
    }

    /**
    个人中心
     * @param uid
     * @param assess_token
     */
    public function user_center($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $field = [
                'uid',
                'username',
                'sex',
                'type',
                'logo',
                'integral',
                'online_id',
                's_id',
                'balance',
                'status_type'
            ];
            $member = Db::name("members")->where('uid',$uid)->field($field)->find();
           
           
            if($member['type']==0 && $member['status_type']==1){
            
                $member['type']=1;
            }
           
            return format_return(1,$member,"数据获取成功");
        }
    }

    /**
    积分
     * @param uid
     * @param assess_token
     */
    public function get_integral($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $member = Db::name("members")->where('uid',$uid)->find();
            $records = Db::name("records")->where('uid',$uid)->select();
            $data['member'] = $member;
            $data['records'] = $records;
            return format_return(1,$data,"数据获取成功");
        }
    }

    /**
    生成二维码
     */
    function code($uid = '',$assess_token = ''){
         if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
                vendor('phpqrcode');
                // 纠错级别：L、M、Q、H
                $level = 'L';
                // 点的大小：1到10,用于手机端4就可以了
                $size = 4;
                $path = "/qrcode/";
                // 生成的文件名
                $fileName = $path.$uid.'.png';
                \QRcode::png($uid,false, $level, $size);
                }
        }
    /**
    评论
     * @param uid
     * @param assess_token
     * @param g_id
     * @param content
     * @param ppic_one
     * @param ppic_two
     * @param ppic_three
     * @param ppic_four
     */
    public function add_comment($uid = '',$assess_token = '',$g_id = 0,$content = '',$ppic_one = '',$ppic_two = '',$ppic_three = '',$ppic_four = '',$time =0){
        header('Access-Control-Allow-Origin:*');
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            empty($g_id) && $this->error("非法参数");
            $where = [
                'is_delete' => 0,
                'id' => $g_id
            ];
            $goods = Db::name("goods")->where($where)->find();
            $data = [
                'uid' => $uid,
                's_id' => $goods['s_id'],
                'g_id' => $g_id,
                'content' => $content,
                'timestamp' => time()
            ];
            $res = Db::name("goods_comment")->insertGetId($data);
            $comment = [];
            if(!empty($ppic_one)){
                $i = sizeof($comment);
                $comment[$i]['picture'] = $ppic_one;
                $comment[$i]['g_id'] = $g_id;
                $comment[$i]['c_id'] = $res;
                $comment[$i]['timestamp'] = time();
            }
            if(!empty($ppic_two)){
                $i = sizeof($comment);
                $comment[$i]['picture'] = $ppic_two;
                $comment[$i]['g_id'] = $g_id;
                $comment[$i]['c_id'] = $res;
                $comment[$i]['timestamp'] = time();
            }
            if(!empty($ppic_three)){
                $i = sizeof($comment);
                $comment[$i]['picture'] = $ppic_three;
                $comment[$i]['g_id'] = $g_id;
                $comment[$i]['c_id'] = $res;
                $comment[$i]['timestamp'] = time();
            }
            if(!empty($ppic_four)){
                $i = sizeof($comment);
                $comment[$i]['picture'] = $ppic_four;
                $comment[$i]['g_id'] = $g_id;
                $comment[$i]['c_id'] = $res;
                $comment[$i]['timestamp'] = time();
            }
            Db::name("comment_picture")->insertAll($comment);
            $goods['comment_num']++;
            Db::name("goods")->where($where)->update(['comment_num' => $goods['comment_num']]);
            $tim = strtotime($time);
            $where_order = [
                'uid' => $uid,
                'g_id' => $g_id,
                'status' => 3,
                'create_timestamp' =>$tim
            ];
            $data_order = [
                'status' => 4
            ];
            Db::name("orders")->where($where_order)->update($data_order);
            return format_return(1,'',"评论成功");
        }
    }
    /**
    评论页面
     * @param uid
     * @param assess_token
     * @param g_id
     */
    public function comment_ye($uid = '',$assess_token = '',$g_id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where = [
                'is_delete' => 0,
                'id' => $g_id,
                'status' => 1
            ];
            $field = [
                'id',
                'subject',
                'cover'
            ];
            $goods = Db::name("goods")->where($where)->field($field)->find();
            return format_return(1,$goods,"数据获取成功");
        }
    }

    public function ass($ppic = ""){
        if(!empty($ppic)){
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $ppic, $result)){
                $type = $result[2];
                $new_file = "/upload/".time().".{$type}";
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $ppic)))){
                    echo '新文件保存成功：', $new_file;
                }
            }
            $data = [
                'name' => $new_file
            ];
            Db::name("log")->insert($data);
        }
    }

    /**
    获取二维码
     * @param uid
     * @param assess_token
     */
    public function get_qrcode($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where = [
                'uid' => $uid,
                'status' => 1
            ];
            $store_unline = Db::name("store_unline")->where($where)->find();
            return format_return(1,$store_unline['qrcode'],"数据获取成功");

        }
    }

    /**
    获取个人信息
     * @param uid
     * @param assess_token
     */
  public function get_member($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
         $field = [
                'username',
                'type',
                'logo'
                
            ];
            $member = Db::name("members")->where('uid',$uid)->field($field)->find();
           
            if(!empty($member)){
                return format_return(1,$member,"数据获取成功");
            }else{
                return format_return(0,'',"数据获取成功");
            }
        }
    }

    /**
    判断是否是会员
     * @param uid
     * @param assess_token
     */
    public function is_member($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $member = Db::name("members")->where('uid',$uid)->find();
            if($member['status_type'] === 0){
                return format_return(0,'','您还不是会员，请先成为会员！');
            }else{
                return format_return(1,'','尊贵的会员');
            }
        }
    }
    /**
    获取扫码门店信息
     * @param uid
     * @param assess_token
     * @param s_id
     */
    public function get_unlinestore($uid = '',$assess_token = '',$s_id = 0){
        
//        dump($s_id);die;
       
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $member_uid = Db::name("members")->where('uid',$uid)->find();
           
            $where = [
                'id' => $s_id,
                'status' => 1
            ];
            $store = Db::name("store_unline")->where($where)->find();
            if(empty($store)){
                return format_return(0, '2', '没有此门店，请正确扫码二维码');
            }
            
            if($member_uid['status_type'] == 0 ){
                return format_return(0,'1','您还不是会员，请先成为会员！');
                
            }
            elseif($member_uid['status_type'] != 0 && $member_uid['store_id'] != $s_id && $member_uid['store_id'] != 0){
                return format_return(0,'2','您不是这家门店的会员，不能在此消费！');
            }
            
              $where_member = [
                    's_id' => $s_id,
                    'type' => 2
                ];
                $member = Db::name("members")->where($where_member)->find();

                $data['name'] = $store['name'];
                $data['logo'] = $member['logo'];
                $data['type'] = $member['type'];
                if (!empty($data)) {
                    Db::name("log")->insert(['name'=>"获取门店信息".$store['name']]);
                    return format_return(1, $data, "数据获取成功");
                } else {
                    return format_return(0, '1', '数据获取失败');
                }
            
        }
    }

    /**
    获取供应商信息
     * @param uid
     * @param assess_token
     */
    public function get_online($uid = '',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where = [
                'a.uid' =>$uid,
                'a.status' => '1'
            ];
            $join = [
                ['kefu m','m.s_id=a.id','left'],
               
            ];
            $online = Db::name("store_online")->alias('a')->join($join)->field('a.*,m.http')->where($where)->find();
            if(empty($online)){
                return format_return(0,"","数据获取失败");
            }else{
                $data['account'] = $online['account'];
                $data['kefuname'] =substr(substr($online['http'], 25),0,-3);
                $data['url'] = "http://www.tianjunhai.com/store";
                return format_return(1,$data,"数据获取成功");
            }
        }
    }
    /**
    版本更新
     * @param uid
     * @param assess_token
     */
    public function banben(){
       $res = Db::name('banben')->where('id=1')->find();
        if(empty($res)){
            return format_return(0,'','数据接收失败');
        }else{
            return format_return(1,$res['name'],'数据接收成功');
        }
    }

    /**
    预约
     * @param uid
     * @param assess_token
     * @param phone
     * @param appointment
     * @param s_id
     * @param a_id
     */
    public function appointment($uid = '',$assess_token = '',$phone = '',$appointment = '',$s_id = '',$a_id = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $where = [
                'id' => $a_id,
                'is_delete' => 0,
                'status' => 0
            ];
            $appointment_store = Db::name("appointment_store")->where($where)->find();
            if(!empty($appointment_store)){
                $data = [
                    'uid' => $uid,
                    'phone' => $phone,
                    's_id' => $s_id,
                    'appointment' => $appointment,
                    'a_id' => $a_id
                ];
                $where_data = [
                    'uid' => $uid,
                    'a_id' => $a_id,
                    's_id' => $s_id
                ];
                $result = Db::name("appointment_user")->where($where_data)->find();
                if(!empty($result)){
                    return format_return(0,'','您已预约');
                }
                $res = Db::name("appointment_user")->insertGetId($data);
                if(!empty($res)){
                    $appointment_store['current']++;
                    if($appointment_store['current'] >= $appointment_store['number']){
                        $appointment_store['status'] = 1;
                    }
                    $update = [
                        'status' => $appointment_store['status'],
                        'current' => $appointment_store['current']
                    ];
                    Db::name("appointment_store")->where($where)->update($update);
                    return format_return(1,'','预约成功');
                }
            }else{
                return format_return(0,'','预约人数已满');
            }
        }
    }

    /**
    预约页面
     * @param s_id
     */
    public function appointment_store($s_id = 0){
        empty($s_id) && $this->error("非法参数");
        $start_time = strtotime(date('Y-m-d',time()));
        //$next_time = $time + 86400;
        $where = [
            's_id' => $s_id,
            'is_delete' => 0
        ];
        $store = Db::name("appointment_store")->where($where)->select();
        $data = [];
        foreach($store as $key => $value){
            $store[$key]['is_selected'] = 0;
        }
        for($i = 0; $i < 7; $i ++){
            $end_time = $start_time + 86400;
            foreach($store as $key => $value){
                if($value['start_time'] >= $start_time && $value['end_time'] < $end_time){
                    //dump($i);
                    if( $store[$key]['is_selected'] == 0){
                        $data[$i][] = $value;
                        $store[$key]['is_selected'] = 1;
                    }
                }
            }
            $start_time = $end_time;
        }
        $unline = Db::name("store_unline")->where('id',$s_id)->find();
        $datas['data'] = $data;
        $datas['store'] = $unline;
        //print_r($store);die;
        if(!empty($datas)){
            return format_return(1,$datas,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }
    /**
    合约金
     */
    public function get_unline_price(){
        $price = Db::name("unline_price")->where('id',1)->find();
        if(!empty($price)){
            return format_return(1,$price,'数据获取成功');
        }else{
            return format_return(0,'','数据为空');
        }
    }
    /**
    会员价格
     */
    public function get_member_price(){
        $price = Db::name("member_price")->select();
        if(!empty($price)){
            return format_return(1,$price,'数据获取成功');
        }else{
            return format_return(0,'','数据为空');
        }
    }

    /**
    点赞
     * @param uid
     * @param assess_token
     * @param s_id
     */
    public function like($uid = 0, $assess_token = '',$s_id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $store = Db::name("store_unline")->where('id',$s_id)->find();
            if(!empty($store)){
                $store['like']++;
                Db::name('store_unline')->where('id',$s_id)->update(['like'=>$store['like']]);
                return format_return(1,'','点赞成功');
            }else{
                return format_return(0,'','点赞失败');
            }
        }
    }

    /**
    预约页面数据
     * @param id
     */
    public function appointment_page($id = 0,$uid ='',$assess_token = ''){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            empty($id) && $this->error("非法参数");
            $member = Db::name("members")->where('uid',$uid)->find();
            $where = [
                'id' => $id,
                'is_delete' => 0
            ];
            $appointment = Db::name("appointment_store")->where($where)->find();
            $appointment['username'] = $member['username'];
            if(!empty($appointment)){
                return format_return(1,$appointment,"数据获取成功");
            }else{
                return format_return(0,'','数据为空');
            }
        }
    }

    /**
    未读预约
     * @param uid
     * @param assess_token
     * @param page
     */
    public function unread($uid ='',$assess_token = '',$page = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $start_time = strtotime(date('Y-m-d',time()));
            $start = $page*10;
            $member = Db::name("members")->where('uid',$uid)->find();
            $where = [
                'a.s_id' => $member['s_id'],
                'a.status' => 0,
                'a.is_delete' => 0
            ];
            $join = [
                ['members m','m.uid=a.uid'],
                ['appointment_store s','s.id=a.a_id']
            ];
            $field = [
                'm.username',
                'a.*',
                's.start_time'
            ];
            $where_unread = [
                'a.s_id' => $member['s_id'],
                'a.status' => 0,
                'a.is_delete' => 0,
                's.end_time'=>array('<',$start_time)
            ];
            $res = Db::name("appointment_user")->alias('a')->join($join)->field('a.id')->where($where_unread)->select();
            if(!empty($res)){
                $ids = array_column($res,'id');
                Db::name("appointment_user")->where('id','in',$ids)->update(['is_delete'=>1]);
            }
            $unread = Db::name("appointment_user")->alias('a')->join($join)->field($field)->where($where)->limit($start,10)->select();
            if(!empty($unread)){
                return format_return(1,$unread,'数据获取成功');
            }else{
                return format_return(0,'','数据为空');
            }
        }
    }
    /**
    已预约
     * @param uid
     * @param assess_token
     * @param page
     */
    public function read($uid ='',$assess_token = '',$page = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $start_time = strtotime(date('Y-m-d',time()));
            $start = 10*$page;
            $member = Db::name("members")->where('uid',$uid)->find();
            $where = [
                'a.s_id' => $member['s_id'],
                'a.status' => 1,
                'a.is_delete' => 0
            ];
            $join = [
                ['members m','m.uid=a.uid'],
                ['appointment_store s','s.id=a.a_id']
            ];
            $field = [
                'm.username',
                'a.*',
                's.start_time'
            ];
            $where_unread = [
                'a.s_id' => $member['s_id'],
                'a.status' => 1,
                'a.is_delete' => 0,
                's.end_time'=>array('<',$start_time)
            ];
            $res = Db::name("appointment_user")->alias('a')->join($join)->field('a.id')->where($where_unread)->select();
            if(!empty($res)){
                $ids = array_column($res,'id');
                Db::name("appointment_user")->where('id','in',$ids)->update(['is_delete'=>1]);
            }
            $unread = Db::name("appointment_user")->alias('a')->where($where)->join($join)->field($field)->limit($start,10)->select();
            if(!empty($unread)){
                return format_return(1,$unread,'数据获取成功');
            }else{
                return format_return(0,'','数据为空');
            }
        }
    }

    /**
    处理预约
     * @param uid
     * @param assess_token
     * @param id
     */
    public function pass_appointment($uid ='',$assess_token = '',$id = 0){
        if(is_token($uid,$assess_token) == 0){
            return format_return(0,'',"验证错误，请重新登录",1);
        }else{
            $update = [
                'status' => 1
            ];
            $where = [
                'id' => $id,
                'is_delete' => 0
            ];
            $res = Db::name("appointment_user")->where($where)->update($update);
            if(!empty($res)){
                return format_return(1,'','处理成功');
            }else{
                return format_return(0,'','处理失败');
            }
        }
    }
}